<template>
  <div>
    <el-container>
      <el-header>
        <div class="logo">
          <img src="@/assets/logo.png" alt="" />
          <span>电商后台系统</span>
        </div>
        <el-button plain size="mini" @click="loginOut">退出登陆</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggel-button" @click="toggelCollapse">|||</div>
          <el-menu :default-active="$route.path" class="el-menu-vertical-demo" background-color="#313743"
            text-color="#fff" active-text-color="rgb(53,155,255)" unique-opened :collapse="isCollapse"
            :collapse-transition="false" :router="true">
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.authName]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + itemSub.path" v-for="itemSub in item.children" :key="itemSub.id">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ itemSub.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
// import {getMenu} from '@/api/menu'

export default {
  name: 'home',
  data () {
    return {
      menuList: [],
      iconObj: {
        用户管理: 'el-icon-user-solid',
        权限管理: 'el-icon-set-up',
        商品管理: 'el-icon-s-goods',
        订单管理: 'el-icon-s-order',
        数据统计: 'el-icon-s-data'
      },
      isCollapse: false //是否折叠
    }
  },
  mounted () {
    this.getMenuList()
  },
  methods: {
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    getMenuList () {
      // getMenu().then((res) => {
      this.$http
        .get('/menus')
        .then(res => {
          res = res.data
          // console.log(res)
          if (res.meta.status == 200) {
            this.menuList = res.data
            console.log('menuList', this.menuList)
          } else {
            this.$message({ type: 'error', message: res.meta.msg })
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    toggelCollapse () {
      //菜单展开于折叠
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: rgb(54, 61, 64);
  color: #fff;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
  }
  img {
    width: 40px;
  }
}

.el-aside {
  background-color: rgb(49, 55, 67);
  color: #333;
  .toggel-button {
    text-align: center;
    color: #409eff;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
  .el-submenu .el-menu-item {
    padding-left: 55px !important;
  }
}
.el-main {
  background-color: #e9eef3;
}
.el-container {
  height: 100vh;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
